SlideShare a Scribd company logo
1 of 15
Download to read offline
紹介
        詳解 ICL
 (Interval Container Library)

2011/2/26 Boost 勉強会 #4 未発表
       @yak_ex / 新 康孝
自己紹介
• 氏名: 新 康孝 (あたらし やすたか)
• Twitter ID: yak_ex
• Web: http://yak3.myhome.cx:8080/junks

• C++ / Perl が主戦場
• 現在、仕事でコードに触れていないので
  競技プログラミング(TopCoder、Codeforces)で
  潤い補充
• 闇の軍団に憧れるただの C++ 好き
  – 今回は ICL のさわりだけ、というかさわりしか分からない
ぼくのかんがえた「ぶーすと」のぶんるい
         アプリより




小規模              大規模




         実装より
ぼくのかんがえた「ぶーすと」のぶんるい
                                       アプリより                ~1.25
                                                     test

                                                            graph
 progress_display た
      んはココ                              thread
小規模                                                                     大規模
      crc              pool                              regex
   tokenizer
      lexical_cast            random

                       function
     array
  timer rational                                 iteratorstype_traits
  any operators
               tuple                   実装より
ぼくのかんがえた「ぶーすと」のぶんるい
   異次元                                     アプリより                   ~1.30
   preprocessor                                            test
                                                                           spirit
                                                       date_time
                                                                   graph
                              filesystem
 progress_display た
      んはココ                                   thread
小規模                format                                                     大規模
      crc              pool                                   regex
   tokenizer
      lexical_cast            random

                       function
     array                                     lambda
  timer rational         multi_array                 iteratorstype_traits
  any optional                                                        mpl
               tuple                       実装より
ぼくのかんがえた「ぶーすと」のぶんるい
   異次元                                       アプリより                      ~1.35
   preprocessor                                                  test asio
   wave                                                      gil
                                                                                spirit
                                                            date_time
                                                                        graph
                            filesystem
 progress_display た
                          statechart      thread
      んはココ                                                    serialization
小規模                format program_options                            interprocess 大規模
      crc              pool                                          regex
   tokenizer                                                        xpressive

      lexical_cast            random
                                                    bimap
                       function                      multi_index
     array                parameter                  lambda           intrusive
                               variant ptr_container         range         fusion
  timer rational
       foreach            multi_array                       iteratorstype_traits
  any optional                                                        typeof mpl
               tuple                          実装より
ぼくのかんがえた「ぶーすと」のぶんるい
   異次元                                       アプリより                      ~1.40
   preprocessor                                                  test asio
   wave                                         accmulators gil
                                                                                spirit
                                                           date_time
                                                                        graph
                            filesystem
 progress_display た
                          statechart      thread
      んはココ                                            serialization
小規模                format program_options        proto       interprocess 大規模
      crc              pool                                          regex
   tokenizer                                                        xpressive

      lexical_cast            random
       scope_exit                                   bimap
                       function unordered            multi_index
     array                parameter                  lambda
                                             flyweight                intrusive
                               variant ptr_container         range         fusion
  timer rational
       foreach            multi_array                       iteratorstype_traits
  any optional                                                        typeof mpl
               tuple                          実装より
ぼくのかんがえた「ぶーすと」のぶんるい
   異次元                                       アプリより                      ~1.45
   preprocessor                                                  test asio
   wave                                    accmulators gil
                                                                         spirit
                                                      date_time
                                                                   graph
                        filesystem property_tree            polygon
 progress_display た                                   msm
                      statechart            thread
      んはココ                                              serialization
小規模       uuid     format program_options          proto       interprocess 大規模
      crc              pool                                          regex
   tokenizer                                                        xpressive

      lexical_cast            random
       scope_exit                                   bimap
                       function unordered            multi_index
     array                parameter                  lambda
                                             flyweight                intrusive
                               variant ptr_container         range         fusion
  timer rational
       foreach            multi_array                       iteratorstype_traits
  any optional                                                        typeof mpl
               tuple                          実装より
ぼくのかんがえた「ぶーすと」のぶんるい
   異次元                                       アプリより                      ~1.46
   preprocessor                                                  test asio
   wave                                    accmulators gil
                                                                         spirit
                                                      date_time
                                                                   graph
                        filesystem property_tree      ICL   polygon
 progress_display た                                   msm
                      statechart            thread
      んはココ                                              serialization
小規模       uuid     format program_options          proto       interprocess 大規模
      crc              pool                                          regex
   tokenizer                                                        xpressive

      lexical_cast            random
       scope_exit                                   bimap
                       function unordered            multi_index
     array                parameter                  lambda
                                             flyweight                intrusive
                               variant ptr_container         range         fusion
  timer rational
       foreach            multi_array                       iteratorstype_traits
  any optional                                                        typeof mpl
               tuple                          実装より
ここで競技コーディングのお時間です
• akira さん(仮名)はパーティーを開くことになりました。N 人
  のお客さんが参加する予定ですが参加時間帯[si, ti)が皆ば
  らばらです。場所の予約のために参加人数の最も多い時間
  帯を知りたいのでプログラムを作って akira さんを助けてあ
  げましょう。

  – 1 ≦ N ≦ 1000
  – -109 ≦ si < ti ≦ 109, si, ti は整数 (i=1,2,…,N)
  – 参加人数が同じ時間帯が隣接している場合、参加者が異なっていて
    も一つの時間帯と見なす
  – 同一参加人数の時間帯が複数ある場合は最長の時間帯を、同じ長
    さの時間帯が複数ある場合は開始時刻がもっとも早い時間帯を返す
  – 入力: N<改行>s1<空白>t1<改行>…sN<空白>tN<改行>
  – 出力:開始時刻<空白>終了時刻<空白>人数<改行>
解答例全文 using ICL
#include <iostream>
#include <algorithm>
#include <utility>                                                                             #include 含めて 25 行
#include <boost/icl/interval_map.hpp>

int main(void)
{
   boost::icl::interval_map<int, int> sum;
   int n;
   std::cin >> n;
   for(int i = 0; i < n; ++i) {
       int s, t;
       std::cin >> s >> t;
       sum += std::make_pair(boost::icl::interval<int>::right_open(s, t), 1);
   }
   typedef boost::icl::interval_map<int, int>::value_type value_type;
   auto it = max_element(sum.begin(), sum.end(), [](const value_type &v1, const value_type &v2) {
       return
          v1.second < v2.second ||
          (v1.second == v2.second && v1.first.upper() - v1.first.lower() < v2.first.upper() - v2.first.lower()) ||
          (v1.second == v2.second && v1.first.upper() - v1.first.lower() == v2.first.upper() - v2.first.lower() &&
                                                                                            v1.first.lower() < v2.first.lower());
   });
   std::cout << it->first.lower() << ' ' << it->first.upper() << ' ' << it->second << std::endl;
   return 0;
}
解答例全文 using ICL
#include <iostream>
#include <algorithm>                             ヘッダのインクルード
#include <utility>
#include <boost/icl/interval_map.hpp>

int main(void)
{
   boost::icl::interval_map<int, int> sum;
   int n;
   std::cin >> n;
   for(int i = 0; i < n; ++i) {
       int s, t;                                                                            入力と登録
       std::cin >> s >> t;
       sum += std::make_pair(boost::icl::interval<int>::right_open(s, t), 1);
   }
   typedef boost::icl::interval_map<int, int>::value_type value_type;                                            最大値取得
   auto it = max_element(sum.begin(), sum.end(), [](const value_type &v1, const value_type &v2) {
       return
          v1.second < v2.second ||
          (v1.second == v2.second && v1.first.upper() - v1.first.lower() < v2.first.upper() - v2.first.lower()) ||
          (v1.second == v2.second && v1.first.upper() - v1.first.lower() == v2.first.upper() - v2.first.lower() &&
                                                                                            v1.first.lower() < v2.first.lower());
   });
   std::cout << it->first.lower() << ' ' << it->first.upper() << ' ' << it->second << std::endl;
   return 0;                                                                                           出力
}
解答例 入力と登録
boost::icl::interval_map<int, int> sum;
int n;
std::cin >> n;
for(int i = 0; i < n; ++i) {                            今回は数だが集合も可能
   int s, t;
   std::cin >> s >> t;                  右開区間 [s, t)
   sum += std::make_pair(boost::icl::interval<int>::right_open(s, t), 1);
}
                 1
                                             1
         +                               +
     1                            1
                                                     split_interval_map
                          interval_map                              1
             2                           1                1
 1                   1
解答例 最大値取得
typedef boost::icl::interval_map<int, int>::value_type value_type;
   auto it = max_element(sum.begin(), sum.end(), [](const value_type &v1, const
    value_type &v2) {
       return
          v1.second < v2.second ||          // 集計値は second
          (v1.second == v2.second &&            // 区間は first (upper(), lower() で境界値)
            v1.first.upper() - v1.first.lower() < v2.first.upper() - v2.first.lower()) ||
          (v1.second == v2.second &&
            v1.first.upper() - v1.first.lower() == v2.first.upper() - v2.first.lower() &&
            v1.first.lower() < v2.first.lower());
   });

 集計された区間の結果を iteration 可能なので普通に max_element で最大値取得
Interval Container Library
•   1.46 で導入
•   開区間、閉区間等全部取り扱い可能
•   区間に対する集合演算、「集計」が可能
•   interval_set,
    interval_map                                         interval_set a
                                                         interval_set b
                                         interval_set a - b
•   区間の統合方法として join, separate, split を選択可能
•   ストリーム出力有り(例:{(1,3][4.6](7,8]})
•   Traits 用意すれば自前の interval を突っ込むことも可能
•   数値に限定されないので例えば
    cotinuous_interval<std::string> w(“a”, “c”) // right_open
    とか書くと a, b で始まる文字列全部を表すことになる。
•   Example がいっぱいなのでそれ見ればOK

More Related Content

Similar to Brief introduction of Boost.ICL

Similar to Brief introduction of Boost.ICL (6)

エキ Py 読書会02 2010/9/7
エキ Py 読書会02 2010/9/7エキ Py 読書会02 2010/9/7
エキ Py 読書会02 2010/9/7
 
エキ Py 読書会02 2章後半
エキ Py 読書会02 2章後半エキ Py 読書会02 2章後半
エキ Py 読書会02 2章後半
 
そしてjsの基礎へ戻る#4
そしてjsの基礎へ戻る#4そしてjsの基礎へ戻る#4
そしてjsの基礎へ戻る#4
 
Rcppのすすめ
RcppのすすめRcppのすすめ
Rcppのすすめ
 
R spec勉強会
R spec勉強会R spec勉強会
R spec勉強会
 
Rの高速化
Rの高速化Rの高速化
Rの高速化
 

More from yak1ex

C++0x in programming competition
C++0x in programming competitionC++0x in programming competition
C++0x in programming competitionyak1ex
 
Introduction to programming competition [revised][PDF]
Introduction to programming competition [revised][PDF]Introduction to programming competition [revised][PDF]
Introduction to programming competition [revised][PDF]yak1ex
 
Introduction to programming competition [revised]
Introduction to programming competition [revised]Introduction to programming competition [revised]
Introduction to programming competition [revised]yak1ex
 
C++0x in programming competition
C++0x in programming competitionC++0x in programming competition
C++0x in programming competitionyak1ex
 
Introduction to programming competition
Introduction to programming competitionIntroduction to programming competition
Introduction to programming competitionyak1ex
 
Impractical Introduction of Boost Spirit Qi [PPT]
Impractical Introduction of Boost Spirit Qi [PPT]Impractical Introduction of Boost Spirit Qi [PPT]
Impractical Introduction of Boost Spirit Qi [PPT]yak1ex
 
Brief introduction of Boost.ICL [PDF]
Brief introduction of Boost.ICL [PDF]Brief introduction of Boost.ICL [PDF]
Brief introduction of Boost.ICL [PDF]yak1ex
 
Brief introduction of Boost.ICL
Brief introduction of Boost.ICLBrief introduction of Boost.ICL
Brief introduction of Boost.ICLyak1ex
 
GC in C++0x [eng]
GC in C++0x [eng]GC in C++0x [eng]
GC in C++0x [eng]yak1ex
 
GC in C++0x
GC in C++0xGC in C++0x
GC in C++0xyak1ex
 

More from yak1ex (10)

C++0x in programming competition
C++0x in programming competitionC++0x in programming competition
C++0x in programming competition
 
Introduction to programming competition [revised][PDF]
Introduction to programming competition [revised][PDF]Introduction to programming competition [revised][PDF]
Introduction to programming competition [revised][PDF]
 
Introduction to programming competition [revised]
Introduction to programming competition [revised]Introduction to programming competition [revised]
Introduction to programming competition [revised]
 
C++0x in programming competition
C++0x in programming competitionC++0x in programming competition
C++0x in programming competition
 
Introduction to programming competition
Introduction to programming competitionIntroduction to programming competition
Introduction to programming competition
 
Impractical Introduction of Boost Spirit Qi [PPT]
Impractical Introduction of Boost Spirit Qi [PPT]Impractical Introduction of Boost Spirit Qi [PPT]
Impractical Introduction of Boost Spirit Qi [PPT]
 
Brief introduction of Boost.ICL [PDF]
Brief introduction of Boost.ICL [PDF]Brief introduction of Boost.ICL [PDF]
Brief introduction of Boost.ICL [PDF]
 
Brief introduction of Boost.ICL
Brief introduction of Boost.ICLBrief introduction of Boost.ICL
Brief introduction of Boost.ICL
 
GC in C++0x [eng]
GC in C++0x [eng]GC in C++0x [eng]
GC in C++0x [eng]
 
GC in C++0x
GC in C++0xGC in C++0x
GC in C++0x
 

Recently uploaded

【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)Hiroki Ichikura
 
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)UEHARA, Tetsutaro
 
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfAWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfFumieNakayama
 
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...博三 太田
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものですiPride Co., Ltd.
 
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案sugiuralab
 
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?akihisamiyanaga1
 
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineerYuki Kikuchi
 
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfクラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfFumieNakayama
 

Recently uploaded (9)

【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
 
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
 
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfAWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
 
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
 
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
 
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
 
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
 
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfクラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
 

Brief introduction of Boost.ICL

  • 1. 紹介 詳解 ICL (Interval Container Library) 2011/2/26 Boost 勉強会 #4 未発表 @yak_ex / 新 康孝
  • 2. 自己紹介 • 氏名: 新 康孝 (あたらし やすたか) • Twitter ID: yak_ex • Web: http://yak3.myhome.cx:8080/junks • C++ / Perl が主戦場 • 現在、仕事でコードに触れていないので 競技プログラミング(TopCoder、Codeforces)で 潤い補充 • 闇の軍団に憧れるただの C++ 好き – 今回は ICL のさわりだけ、というかさわりしか分からない
  • 3. ぼくのかんがえた「ぶーすと」のぶんるい アプリより 小規模 大規模 実装より
  • 4. ぼくのかんがえた「ぶーすと」のぶんるい アプリより ~1.25 test graph progress_display た んはココ thread 小規模 大規模 crc pool regex tokenizer lexical_cast random function array timer rational iteratorstype_traits any operators tuple 実装より
  • 5. ぼくのかんがえた「ぶーすと」のぶんるい 異次元 アプリより ~1.30 preprocessor test spirit date_time graph filesystem progress_display た んはココ thread 小規模 format 大規模 crc pool regex tokenizer lexical_cast random function array lambda timer rational multi_array iteratorstype_traits any optional mpl tuple 実装より
  • 6. ぼくのかんがえた「ぶーすと」のぶんるい 異次元 アプリより ~1.35 preprocessor test asio wave gil spirit date_time graph filesystem progress_display た statechart thread んはココ serialization 小規模 format program_options interprocess 大規模 crc pool regex tokenizer xpressive lexical_cast random bimap function multi_index array parameter lambda intrusive variant ptr_container range fusion timer rational foreach multi_array iteratorstype_traits any optional typeof mpl tuple 実装より
  • 7. ぼくのかんがえた「ぶーすと」のぶんるい 異次元 アプリより ~1.40 preprocessor test asio wave accmulators gil spirit date_time graph filesystem progress_display た statechart thread んはココ serialization 小規模 format program_options proto interprocess 大規模 crc pool regex tokenizer xpressive lexical_cast random scope_exit bimap function unordered multi_index array parameter lambda flyweight intrusive variant ptr_container range fusion timer rational foreach multi_array iteratorstype_traits any optional typeof mpl tuple 実装より
  • 8. ぼくのかんがえた「ぶーすと」のぶんるい 異次元 アプリより ~1.45 preprocessor test asio wave accmulators gil spirit date_time graph filesystem property_tree polygon progress_display た msm statechart thread んはココ serialization 小規模 uuid format program_options proto interprocess 大規模 crc pool regex tokenizer xpressive lexical_cast random scope_exit bimap function unordered multi_index array parameter lambda flyweight intrusive variant ptr_container range fusion timer rational foreach multi_array iteratorstype_traits any optional typeof mpl tuple 実装より
  • 9. ぼくのかんがえた「ぶーすと」のぶんるい 異次元 アプリより ~1.46 preprocessor test asio wave accmulators gil spirit date_time graph filesystem property_tree ICL polygon progress_display た msm statechart thread んはココ serialization 小規模 uuid format program_options proto interprocess 大規模 crc pool regex tokenizer xpressive lexical_cast random scope_exit bimap function unordered multi_index array parameter lambda flyweight intrusive variant ptr_container range fusion timer rational foreach multi_array iteratorstype_traits any optional typeof mpl tuple 実装より
  • 10. ここで競技コーディングのお時間です • akira さん(仮名)はパーティーを開くことになりました。N 人 のお客さんが参加する予定ですが参加時間帯[si, ti)が皆ば らばらです。場所の予約のために参加人数の最も多い時間 帯を知りたいのでプログラムを作って akira さんを助けてあ げましょう。 – 1 ≦ N ≦ 1000 – -109 ≦ si < ti ≦ 109, si, ti は整数 (i=1,2,…,N) – 参加人数が同じ時間帯が隣接している場合、参加者が異なっていて も一つの時間帯と見なす – 同一参加人数の時間帯が複数ある場合は最長の時間帯を、同じ長 さの時間帯が複数ある場合は開始時刻がもっとも早い時間帯を返す – 入力: N<改行>s1<空白>t1<改行>…sN<空白>tN<改行> – 出力:開始時刻<空白>終了時刻<空白>人数<改行>
  • 11. 解答例全文 using ICL #include <iostream> #include <algorithm> #include <utility> #include 含めて 25 行 #include <boost/icl/interval_map.hpp> int main(void) { boost::icl::interval_map<int, int> sum; int n; std::cin >> n; for(int i = 0; i < n; ++i) { int s, t; std::cin >> s >> t; sum += std::make_pair(boost::icl::interval<int>::right_open(s, t), 1); } typedef boost::icl::interval_map<int, int>::value_type value_type; auto it = max_element(sum.begin(), sum.end(), [](const value_type &v1, const value_type &v2) { return v1.second < v2.second || (v1.second == v2.second && v1.first.upper() - v1.first.lower() < v2.first.upper() - v2.first.lower()) || (v1.second == v2.second && v1.first.upper() - v1.first.lower() == v2.first.upper() - v2.first.lower() && v1.first.lower() < v2.first.lower()); }); std::cout << it->first.lower() << ' ' << it->first.upper() << ' ' << it->second << std::endl; return 0; }
  • 12. 解答例全文 using ICL #include <iostream> #include <algorithm> ヘッダのインクルード #include <utility> #include <boost/icl/interval_map.hpp> int main(void) { boost::icl::interval_map<int, int> sum; int n; std::cin >> n; for(int i = 0; i < n; ++i) { int s, t; 入力と登録 std::cin >> s >> t; sum += std::make_pair(boost::icl::interval<int>::right_open(s, t), 1); } typedef boost::icl::interval_map<int, int>::value_type value_type; 最大値取得 auto it = max_element(sum.begin(), sum.end(), [](const value_type &v1, const value_type &v2) { return v1.second < v2.second || (v1.second == v2.second && v1.first.upper() - v1.first.lower() < v2.first.upper() - v2.first.lower()) || (v1.second == v2.second && v1.first.upper() - v1.first.lower() == v2.first.upper() - v2.first.lower() && v1.first.lower() < v2.first.lower()); }); std::cout << it->first.lower() << ' ' << it->first.upper() << ' ' << it->second << std::endl; return 0; 出力 }
  • 13. 解答例 入力と登録 boost::icl::interval_map<int, int> sum; int n; std::cin >> n; for(int i = 0; i < n; ++i) { 今回は数だが集合も可能 int s, t; std::cin >> s >> t; 右開区間 [s, t) sum += std::make_pair(boost::icl::interval<int>::right_open(s, t), 1); } 1 1 + + 1 1 split_interval_map interval_map 1 2 1 1 1 1
  • 14. 解答例 最大値取得 typedef boost::icl::interval_map<int, int>::value_type value_type; auto it = max_element(sum.begin(), sum.end(), [](const value_type &v1, const value_type &v2) { return v1.second < v2.second || // 集計値は second (v1.second == v2.second && // 区間は first (upper(), lower() で境界値) v1.first.upper() - v1.first.lower() < v2.first.upper() - v2.first.lower()) || (v1.second == v2.second && v1.first.upper() - v1.first.lower() == v2.first.upper() - v2.first.lower() && v1.first.lower() < v2.first.lower()); }); 集計された区間の結果を iteration 可能なので普通に max_element で最大値取得
  • 15. Interval Container Library • 1.46 で導入 • 開区間、閉区間等全部取り扱い可能 • 区間に対する集合演算、「集計」が可能 • interval_set, interval_map interval_set a interval_set b interval_set a - b • 区間の統合方法として join, separate, split を選択可能 • ストリーム出力有り(例:{(1,3][4.6](7,8]}) • Traits 用意すれば自前の interval を突っ込むことも可能 • 数値に限定されないので例えば cotinuous_interval<std::string> w(“a”, “c”) // right_open とか書くと a, b で始まる文字列全部を表すことになる。 • Example がいっぱいなのでそれ見ればOK